<?php

use Drupal\distribution\Entity\DistributionApply;
use Drupal\distribution\Entity\Distribution;

function distribution_apply_form($form, &$form_state, DistributionApply $distributionApply, $op) {
  $form_state['distributionApply'] = $distributionApply;
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'distribution') . '/distribution.forms.inc';

  $edit = FALSE;
  if ($op == 'edit') {
    $edit = TRUE;
  }
  $form['user'] = [
    '#type' => 'fieldset',
    '#title' => t('申请信息'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  ];
  $form['user']['name'] = [
    '#title' => t('姓名'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#default_value' => $distributionApply->name,
    '#disabled' => $edit,
  ];
  $form['user']['tel'] = [
    '#title' => t('手机号'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#default_value' => $distributionApply->name,
    '#disabled' => $edit,
  ];
  $form['user']['birthday'] = [
    '#title' => t('生日'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#default_value' => $distributionApply->birthday,
    '#disabled' => $edit,
  ];
  $form['logs'] = [
    '#title' => t('理由记录'),
    '#type' => 'textarea',
    '#description' => t('如果选择不通过这个申请，请输入理由。'),
  ];
  $form['_op'] = ['#type' => 'value', '#value' => $op];
  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('通过'),
    '#submit' => ['distribution_apply_form_submit_apply'],
  ];
  $form['actions']['delete'] = [
    '#type' => 'submit',
    '#value' => t('不通过'),
    '#submit' => ['distribution_apply_form_submit_no_apply'],
  ];

  return $form;
}

function distribution_apply_form_submit_apply($form, &$form_state) {
  $distributionApply = $form_state['distributionApply'];
  if ($distributionApply instanceof DistributionApply) {
    global $user;
    $distributionApply->op_uid = $user->uid;
    $distributionApply->status = 1;
    $distributionApply->log = $form_state['values']['logs'];
    $distributionApply->save();

    $account = user_load($distributionApply->uid);
    if (!empty($account)) {
      $role = user_role_load_by_name('小程序推客');
      $account->roles[$role->rid] = $role->rid;
      user_save($account);
    }

  }
}

function distribution_apply_form_submit_no_apply($form, &$form_state) {
  $distributionApply = $form_state['distributionApply'];
  if ($distributionApply instanceof DistributionApply) {
    global $user;
    $distributionApply->op_uid = $user->uid;
    $distributionApply->status = 2;
    $distributionApply->log = $form_state['values']['logs'];
    $distributionApply->save();
  }
}


function distribution_distribution_entity_form($form, &$form_state, Distribution $distribution) {
  $form_state['distribution'] = $distribution;
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'distribution') . '/distribution.forms.inc';

  $form['parent'] = [
    '#type' => 'textfield',
    '#title' => t('上家'),
    '#default_value' => $distribution->pid,
  ];
  $form['team'] = [
    '#type' => 'textfield',
    '#title' => t('团队'),
    '#default_value' => $distribution->tid,
  ];

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function distribution_distribution_entity_form_submit($form, &$form_state) {
  $db_transaction = db_transaction();
  try {
    $distribution = $form_state['distribution'];
    if ($distribution instanceof Distribution) {
      $pid = $form_state['values']['parent'];
      $tid = $form_state['values']['team'];
      $distribution->pid = $pid;
      $distribution->tid = $tid;
      $distribution->save();
    }
  } catch (Exception $e) {
    $db_transaction->rollback();
    watchdog_exception(__FUNCTION__, $e);
  }
}
